package org.lzy.shop.view

import android.app.Activity
import android.text.TextUtils
import org.lzy.shop.Constant
import top.zibin.luban.CompressionPredicate
import top.zibin.luban.Luban
import top.zibin.luban.OnCompressListener
import java.io.File
import java.lang.ref.WeakReference
import java.util.Locale

class TakePhoto(private val mActivity: WeakReference<Activity>) {
    //拍照后压缩图片并上传，大小约300多K
    fun compressPicture(paths: String?, listener: OnCompressListener?) {
        val activity = mActivity.get() ?: return
        if (!Constant.pictureFile.exists()) {
            val file = File(Constant.pictureFile.getPath())
            val mkdir = file.mkdirs()
            if (!mkdir) {
            }
        }
        // 传人要压缩的图片列表
        Luban.with(activity)
            .load(paths) //不压缩的阈值，单位为K
            // 忽略不压缩图片的大小
            .ignoreBy(100)
            .setTargetDir(Constant.pictureFilePath).filter(object : CompressionPredicate {
                override fun apply(path: String?): Boolean {
                    return !(TextUtils.isEmpty(path) || path!!.lowercase(Locale.getDefault())
                        .endsWith(".gif"))
                }
            }) // 设置压缩后文件存储位置
            .setCompressListener(listener).launch()
    }


}
